package site.wlwsjsx.iot_backend.service.impl;

import org.springframework.stereotype.Service;
import site.wlwsjsx.iot_backend.entity.UserInfo;
import site.wlwsjsx.iot_backend.mapper.UserMapper;
import site.wlwsjsx.iot_backend.service.UserService;

@Service
public class UserServiceImpl implements UserService {
    final
    UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public int txSaveUser(UserInfo userInfo) {
        return userMapper.insertUser(userInfo);
    }

    @Override
    public int txRemoveUserById(int id) {
        return 0;
    }

    @Override
    public int txChangeUser(UserInfo userInfo) {
        return 0;
    }

    @Override
    public UserInfo findUserByUsername(String username) {
        return userMapper.selectUserByUsername(username);
    }

    @Override
    public UserInfo findUserById(Integer userId) {
        // 参数校验
        if (userId == null || userId <= 0) {
            return null;
        }
        try {
            // 直接从数据库查询用户信息
            UserInfo userInfo = userMapper.selectUserById(userId);
            userInfo.setPassword(null);
            userInfo.setSalt(null);
            return userInfo;
        } catch (Exception e) {
            // 记录异常日志
            System.err.println("查询用户信息失败: " + e.getMessage());
            return null;
        }
    }
}